import Vue from 'vue'
import Vuex from 'vuex'
import httpInterface from "./utils/httpInterface";
import { errorMessage } from "./utils/validate";

//挂载Vuex
Vue.use(Vuex)

//创建VueX对象
const store = new Vuex.Store({
  state:{
    courseList: [],
    gradeList: [],
    pageData: {
      page: 1,
      size: 5000
    },
    footerText: "© 2020  南京市莫愁中等专业学校"
  },

  mutations: {
    changeCourseList(state, data) {
      state.courseList = data;
    },

    changeGradeList(state, data) {
      state.gradeList = data;
    }
  },

  actions: {
    getCourseList(state) {
      let params = state.state.pageData;
      httpInterface.requestPost(httpInterface.URL_SUBJECT_LIST, params, res => {
        let { code, body } = res.data;
        if (code == 200) {
          state.commit("changeCourseList", body.rows);
        } else {
          errorMessage("课程加载失败!");
        }
      }, null, true)
    },

    getGradeList(state) {
      httpInterface.requestGet(httpInterface.URL_CLASS_ALL, null, res => {
        let { code, body } = res.data;
        if (code == 200) {
          state.commit("changeGradeList", body);
        } else {
          errorMessage("班级加载失败!");
        }
      }, null)
    },
  }
})

export default store
